package com.xll.controller;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.xll.entity.SysFile;
import com.xll.service.PDFService;
import com.xll.service.SysUserService;
import com.xll.utils.*;
import com.xll.vo.MailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Random;

/**
 * 工具控制器
 */
@RestController
@Api(tags = "其他事项")
@RequestMapping("/tool")
public class ToolController {

    @Autowired
    private QiniuUtils qiniuUtils;

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private SysUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private PDFService pdfService;

    @ApiOperation(value = "七牛云文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile file) throws IOException {
      String url= qiniuUtils.upload(file.getInputStream(),file.getOriginalFilename());
      return  Result.success("文件上传成功",url);
    }

    @ApiOperation(value = "忘记密码?邮件找回")
    @PostMapping("/forget/password")
    public Result forget(@RequestBody MailVo mail){
         mail.setSubject("旅游信息系统");
         Random random=new Random();
         int password=100000+random.nextInt(1000000);
         userService.updatePwdByMail(mail.getReceivers()[0],passwordEncoder.encode(MD5Utils.md5(String.valueOf(password))));
         mail.setContent("您的新密码:"+password+",请妥善保管！");
         return  Result.success(mailUtils.sendmail(mail));
    }


    @ApiOperation(value ="pdf打印" )
    @GetMapping("/pdf/print")
    public void PDFControl(HttpServletResponse response,String query) throws Exception {
        response.setContentType("application/pdf");
        System.out.println(response.getContentType());
        OutputStream os = response.getOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(os));
        pdfService.PDF(pdfDoc,query);
        pdfDoc.close();
    }

    @ApiOperation(value ="pdf打印内科检查信息" )
    @GetMapping("/pdf/printInner")
    public void PDFInnerInfo(HttpServletResponse response,String query) throws Exception {
        response.setContentType("application/pdf");
        System.out.println(response.getContentType());
        OutputStream os = response.getOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(os));
        pdfService.InnerInfo(pdfDoc,query);
        pdfDoc.close();
    }

    @ApiOperation(value ="pdf打印肾脏检查信息" )
    @GetMapping("/pdf/kidney")
    public void PDFKidneyInfo(HttpServletResponse response,String query) throws Exception {
        response.setContentType("application/pdf");
        System.out.println(response.getContentType());
        OutputStream os = response.getOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(os));
        pdfService.KidneyInfo(pdfDoc,query);
        pdfDoc.close();
    }

    @ApiOperation(value ="pdf打印血液检查信息" )
    @GetMapping("/pdf/cell")
    public void PDFCellInfo(HttpServletResponse response) throws Exception {
        response.setContentType("application/pdf");
        System.out.println(response.getContentType());
        OutputStream os = response.getOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(os));
        pdfService.CellInfo(pdfDoc);
        pdfDoc.close();
    }


    @ApiOperation(value ="文件下载" )
    @RequestMapping(value = "/downLoadDailyByUrl")
    public void downLoadUrl(HttpServletRequest request, HttpServletResponse res,
                            String name, String url, SysFile sysFile) throws Exception
    {

        String uploadUrl="http://xllbyte.top/";
        //根据网络文件地址创建URL
        URL daily = new URL(uploadUrl + sysFile.getFileName());
        //获取此路径的连接
        URLConnection conn = daily.openConnection();
        InputStream in = conn.getInputStream();
        res.setContentType("application/force-download; charset=UTF-8");
        String file_name = URLEncoder.encode(sysFile.getFileName(), "UTF-8");
        res.setHeader("Content-Disposition",
                "attachment;filename=" + file_name);
        OutputStream out = res.getOutputStream();
        IOUtils.copyLarge(in, out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);

    }
}
